package cn.zhp.mapper;

import cn.zhp.entity.SysArea;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

public interface SysAreaMapper extends Mapper<SysArea> {




    /**
     * 根据父区域id查找所有区域
     * @return
     */
    @SelectProvider(type = SysAreaSqlProvider.class,method = "selectByPid")
    List<SysArea> selectByPid(Map<String,Object> params);

    @Select("select  " +
            " sub.*,parent.name parentName " +
            "from " +
            " sys_area sub,sys_area parent " +
            "where " +
            " sub.parent_id=parent.id " +
            "and " +
            " sub.id=#{id}")
    SysArea selectByAid(long id);

    /**
     * 根据父级节点id和父级节点的parent_ids更新子级节点的parent_ids   如 猎德大道的修改  0,1,3,5
     * oldParentIds:需要被替换的字符串数据  是旧的父级区域的parentIds   需要替换的字符串是: 0,1,3,
     * concat(#{parentIds},#{parentId},',' :替换后的新字符串          '0,1,2,' +'3,'
     * @return
     */
    @Update("update sys_area set parent_ids=REPLACE(parent_ids,#{oldParentIds},#{parentIds}) " +
            "where parent_ids like CONCAT('%,',#{id},',%')")
    int updateParentIds(SysArea sysArea);

    @InsertProvider(type = SysAreaSqlProvider.class,method = "insertBatch")
    int insertBatch(@Param("sysAreas") List<SysArea> sysAreas);//批量插入数据到数据库

}